<?php


namespace App\Extensions\Exporters;


use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class PatentExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '专利列表.xlsx';

    protected $columns = [
        'uuid'                  => '登记编号',
        'patent_sn'             => '专利号',
        'patent_name'           => '专利名称',
        'patent_type_id'        => '专利类型',
        'legal_status'          => '法律状态',
        'apply_date'            => '申请日',
        'publication_num'       => '公告号',
        'publication_date'      => '授权日',
        'patent_person'         => '专利权人',
        'inventor'              => '发明人',
        'apply.charge_person'   => '负责人',
        'department_id'         => '部门',
        'apply.contact_person'  => '联系人',
        'apply.phone'           => '联系电话',
        'apply.other_applicant' => '合作申请单位',
    ];

    public function __construct(Grid $grid = null)
    {
        parent::__construct($grid);
    }

    public function map($row): array
    {
        $phone = $row->apply->phone ?? '';
        return [
            $row->uuid . "\t",
            $row->patent_sn . "\t",
            $row->patent_name,
            data_get(patent_type_select_option(), $row->patent_type_id),
            $row->legal_status,
            $row->apply_date . "\t",
            $row->publication_num . "\t",
            $row->publication_date . "\t",
            $row->patent_person,
            $row->inventor,
            $row->apply->charge_person ?? '',
            $row->department->name ?? '',
            $row->apply->contact_person ?? '',
            $phone . "\t",
            $row->apply->other_applicant ?? '',
        ];
    }

    public function setFileName($name)
    {
        $this->fileName = $name . '.xlsx';
        return $this;
    }


}
